package com.zhiyan.say.service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhiyan.common.util.model.Page;
import com.zhiyan.say.dao.CommentaryDao;
import com.zhiyan.say.domain.entity.Commentary;

@Service
@Transactional
public class CommentaryServiceImpl implements CommentaryService {
	@Autowired 
	private CommentaryDao commentaryDao;
	
	@Override
	public void add(Commentary commentary){
		try {
			String id = "comm" + UUID.randomUUID().toString().replace("-", "");
			commentary.setId(id);
			commentaryDao.add(commentary);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public void update(Commentary commentary) {
		try {
			commentaryDao.update(commentary);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		
	}

	@Override
	public List<Commentary> query(Map<String, Object> map) {
		try {
			List<Commentary> list = commentaryDao.query(map);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public List<Commentary> queryPage(Page page, Map<String, Object> map) {
		try {
			List<Commentary> list = commentaryDao.query(map);
			int total = list.size();
			page.setTotal(total);
			
			int pageNo = page.getPageNo();
			int pageSize = page.getPageSize();
			int start = (pageNo-1)*pageSize;
			int pageTotal = total%pageSize==0?total/pageSize:total/pageSize+1;
			page.setPageTotal(pageTotal);
			
			map.put("start", start);
			map.put("size", pageSize);
			list = commentaryDao.query(map);
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public void reStatus(String status, List<String> idList) {
		try {
			commentaryDao.reStatus(status, idList);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}


}
